/**
 * @file    Expression classname is undefined on line 2, column 15 in Templates/Classes/Class.java.
 * @author  Haseeb Yousaf
 * @see     LICENSE (MIT style license file)
 * @version 1.0
 * @date    Nov 15, 2011 9:12:52 PM
 */
package carrello.impl.controller.administrator;

import carrello.exception.CarrelloException;
import com.opensymphony.webwork.components.AbstractRichtexteditorConnector.File;
import carrello.model.association.AssociationFactory;
import java.util.LinkedList;
import carrello.model.association.AttributeValue;
import java.util.Iterator;
import carrello.impl.controller.AbstractCTRL;
import org.hibernate.Session;
import carrello.model.entities.Attribute;
import carrello.model.entities.Category;
import carrello.model.entities.Product;
import java.util.List;
import java.util.Map;
import static carrello.util.EntityManagerUtil.getEntityManager;

public class AddProductCTRL extends AbstractCTRL {
    public static LinkedList<LinkedList<String>> fetchProductProperties (String categoryName){
        LinkedList<LinkedList<String>> map = new LinkedList<LinkedList<String>> ();
        
        LinkedList<String> col = new LinkedList<String> ();
        col.add ("Name");
        col.add ("1");
        col.add ("String");
        map.add (col);
        
        col = new LinkedList<String> ();
        col.add ("Description");
        col.add ("1");
        col.add ("String");
        map.add (col);
        
        col = new LinkedList<String> ();
        col.add ("Price");
        col.add ("1");
        col.add ("Double");
        map.add (col);
        
        col = new LinkedList<String> ();
        col.add ("Manufacturer");
        col.add ("1");
        col.add ("String");
        map.add (col);
        
        col = new LinkedList<String> ();
        col.add ("Quantity");
        col.add ("1");
        col.add ("Integer");
        map.add (col);
        
        col = new LinkedList<String> ();
        col.add ("CatalogId");
        col.add ("1");
        col.add ("Integer");
        map.add (col);
        
        List<Attribute> ls = Category.getAttributesByCategoryId(categoryName);
        for ( Attribute l: ls ) {
            col = new LinkedList<String> ();
            col.add (l.getName());
            col.add (String.valueOf(l.getRequired()));
            col.add (l.getType());
            map.add (col);
        }
        return map;
    }

    public static void addProduct(Product p, Map<String, String> map) throws CarrelloException {
        Product test = Product.getExactProductByName(p.getName());
        if ( test != null ) 
            throw new CarrelloException ("Constraint. Product with this name already exists");
        
        if ( Double.valueOf(p.getPrice()) < 0.00 )
            throw new CarrelloException ("Constraint. Price has to be greater than or equal to zero");
        
        if ( p.getQuantity() < 0 )
            throw new CarrelloException ("Constraint. Quantity has to be greater than or equal to zero");
        
        getEntityManager().beginUnitOfWork();
        getEntityManager().store(p);

        Iterator it = map.entrySet().iterator();
        while ( it.hasNext()) {
            Map.Entry pairs = (Map.Entry) it.next();
            Attribute a = Attribute.getAttributeByName(pairs.getKey().toString());
            if ( a == null )
                continue;
            AttributeValue av = new AssociationFactory().createAttributeValue (a, p, pairs.getValue().toString());
            getEntityManager().store(av);
        }
        getEntityManager().endUnitOfWork();
    }

    public static Category getCategory(String categoryid) {
        Category c = Category.getCategoryByName(categoryid);
        return c;
    }
}
